package com.op.parcel.common.utils;

import com.op.parcel.common.api.R;
import com.op.parcel.common.exception.BusinessException;
import org.springframework.util.StringUtils;

/**
 * 数据校验
 *
 * @author xuan🐽
 */
public class AssertUtil {

	public static void isNull(Object object, String message) {
		if (null != object) {
			fail(message);
		}
	}

	public static void isBlank(String str, String message) {
		if (!StringUtils.isEmpty(str)) {
			fail(message);
		}
	}

	public static void notNull(Object object, String message) {
		if (null == object) {
			fail(message);
		}
	}

	public static void notNull(Object object, String message, String code) {
		if (null == object) {
			throw new BusinessException(R.error(message, code));
		}
	}

	public static void notBlank(String str, String message) {
		if (StringUtils.isEmpty(str)) {
			fail(message);
		}
	}

	public static void notBlank(String str, String message, String code) {
		if (StringUtils.isEmpty(str)) {
			throw new BusinessException(R.error(message, code));
		}
	}

	public static void isTrue(boolean value, String message) {
		if (!value) {
			fail(message);
		}
	}

	public static void equals(String a, String b, String message) {
		if (!a.equals(b)) {
			fail(message);
		}
	}

	private static void fail(String message) {
		throw new BusinessException(R.error(message));
	}


	public static void notZero(Integer value, String message) {
		if (value <= 0) {
			fail(message);
		}
	}


}
